<?php
class Admin_LoginController extends Main_Controller_AdminAction
{
	/**
	 * @var App_User_Form_Login
	 */
	private $_formAdminLogin;
	
	/**
     * @return void
     */
    public function init() 
    {
    	$this->_serviceUser = new App_User_Service_User();
    	parent::init();
    }
	
	public function indexAction() 
	{
		$form = Main_Form::factory('User_Form_AdminLogin');
		
		if ($this->_request->isPost()) {
			$data = $this->_getAllParams();
			
            $adapter = new Main_Auth_Adapter_DbTable(Main_Db::get());
            $adapter->setTableName('user')
            	->setIdentityColumn('login')
            	->setCredentialColumn('password')
            	->setIdentity($data['login'])
            	->setCredential(Main_Function::encodePassword($data['password']));	

            $auth = Zend_Auth::getInstance();
			$result = $auth->authenticate($adapter);
			
			$this->view->notificationMessages = array();
			
			if ($result->isValid()) {
				
				$auth->getStorage()->write($adapter->getResultRowObject(null,"password"));
				$user = $auth->getIdentity();
				
				if ($user->f_active == 0) {
					$this->view->notificationMessages[0] = "Ваша учетная запись не активна";
					$this->view->failedAuthentication = true;
				} else {
					$this->_redirect("/admin/index/index" . Main_Const::getSeriveUrlStr());
				}
			} else {
				$this->view->failedAuthentication = true;
				$this->view->notificationMessages[0] = 'Введенные данные неверны';
			}
		}    
		
		$this->view->form = $form;
		return $this->render();
		
	}		
	
}
